package com.ibm.db2oncampus.portal.framework.beans;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.myfaces.shared_impl.util.MessageUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ibm.db2oncampus.portal.usuario.domain.Person;

/**
 * 
 * @author Wellington
 *
 */
public abstract class FacesBeanSupport {

    public ApplicationContext getApplicationContext()
    {
        return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    }

    protected FacesContext getFacesContext()
    {
        return FacesContext.getCurrentInstance();
    }

    protected HttpServletResponse getResponse()
    {
        return (HttpServletResponse)getFacesContext().getExternalContext().getResponse();
    }

    protected HttpServletRequest getRequest()
    {
        return (HttpServletRequest)getFacesContext().getExternalContext().getRequest();
    }

    protected ServletContext getServletContext()
    {
        return (ServletContext)getFacesContext().getExternalContext().getContext();
    }

    public String getParameter(String name)
    {
        return getRequest().getParameter(name);
    }

    protected HttpSession getSession()
    {
        return getRequest().getSession();
    }

    public ResourceBundle getBundle()
    {
        return ResourceBundle.getBundle(getBundleName(), getRequest().getLocale());
    }

    public String getBundleName()
    {
        return getFacesContext().getApplication().getMessageBundle();
    }

    public String getMessage(String key)
    {
        try
        {
            return getBundle().getString(key);
        }
        catch(MissingResourceException mre)
        {
        }
        return "";
    }

    public String getMessage(String key, String arg[])
    {
        MessageFormat form = new MessageFormat(getMessage(key));
        return form.format(arg);
    }

    protected void addMessage(String key, String args[])
    {
        MessageUtils.addMessage(FacesMessage.SEVERITY_INFO, key, args, (String)null);
    }

    protected void addError(String key, String args[])
    {
        MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR, key, args);
    }

    public void reset()
    {
    }
    
    protected Person getLoggedUser() {
    	HttpSession httpSession = (HttpSession) this.getFacesContext().getExternalContext().getSession(false);
		Person loggedUser = (Person) httpSession.getAttribute("loggedUser");
		return loggedUser;
    }
}